package anji.plus.system.mark.common.web;

import anji.plus.system.mark.base.Header;
import anji.plus.system.mark.domain.SysUser;
import anji.plus.system.mark.dto.LoginInfo;
import anji.plus.system.mark.utils.LoginUtils;
import anji.plus.system.mark.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author luwei
 */
@Component
public class GlobalInterceptor implements HandlerInterceptor {

	@Resource
	LoginUtils loginUtils;
	@Resource
	RedisUtil redisUtil;

	private static final Logger LOGGER = LoggerFactory.getLogger(GlobalInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
		LOGGER.info("url"+request.getRequestURL());
		String accessToken = Header.getAuthorization(request.getHeader("Authorization"));
	//		String name = request.`````````````````````````````````````
		SysUser usr = loginUtils.get(accessToken);
		if (usr != null) {
			redisUtil.expire("loginInfo:" + accessToken, 180000);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView model) {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
	}

}
